<template>
	<view>
		<!-- #ifdef MP -->
		<view>
			<text selectable="true" userSelect="true">
				小程序可以直接通过授权获取用户信息，不需要手动填写，获取到的信息JSON格式化后字符串是:
			</text>
			<rich-text :nodes="wxUserStr">
			</rich-text>
		</view>
		<!-- #endif -->
		<!-- #ifdef H5 || APP-PLUS -->
		<view>
			<form>
				<view class="inline">
					<text>姓名</text><input class="input" v-model="user.name">
				</view>
				<view class="inline">
					<text>住址</text><input class="input" v-model="user.addr">
				</view>
				<view class="inline">
					<text>性别</text>
					<radio-group class="uni-flex" v-model="user.sex">
						<label><radio color="#f09625" value="0" :checked="user.sex == 0"/>男</label>
						<label><radio color="#f09625" value="1" :checked="user.sex == 1"/>女</label>
					</radio-group>
				</view>
			</form>
		</view>
		<button @click="login">登录</button>
		<!-- #endif -->
	</view>
</template>

<script>
	//这里演示条件编译，以用户登录为例，
	export default {
		data() {
			return {
				user:{
					name:null,
					sex:0,
					addr:null
				},
				wxUserStr:null
			}
		},
		onLoad(){
			this.login()
		},
		methods: {
			login(){
				// #ifdef MP
				uni.getUserInfo({
					withCredentials:false,
					success:(res1)=>{
						console.log('wxLoginSuccess',JSON.stringify(res1))
						if(res1.errMsg!='getUserInfo:ok'){
							uni.showToast({title:'必须授权用户信息',icon:'none'})		
							return 
						}
						let v = JSON.stringify(res1.userInfo)
						this.wxUserStr = v
						console.log('wx',this.wxUserStr)
						
						//触发全局事件
						uni.$emit('wxLogin',{name:res1.userInfo.nickName})
					}
				})
				// #endif
			}
		}
	}
</script>

<style>

</style>
